package com.changgou.util;

import com.changgou.FastDFSFile;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

public class FastDFSClient2 {

    static {
        String path = "fdfs_client.conf";
        String fdfsClient = new ClassPathResource(path).getPath();
        try {
            ClientGlobal.init(fdfsClient);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    //上传
    public static String[] uploadFiles(FastDFSFile fastDFSFile) {
        try {
            String groupName = fastDFSFile.getName();
            byte[] fileExt = fastDFSFile.getContent();
            NameValuePair[] metaList = new NameValuePair[1];
            metaList[0] = new NameValuePair(fastDFSFile.getAuthor());
            TrackerServer trackerServer = new TrackerClient().getConnection();
            StorageClient storageClient = new StorageClient(trackerServer, null);
            String[] strings = storageClient.upload_file(fileExt, groupName,metaList);
            return strings;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }
}
